//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CCTEU.h"
using namespace Cephei::QL::Instruments::Bonds;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/Bonds/FloatingRateBond.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::Bonds::CCCTEU::CCCTEU (DateTime maturityDate, Double spread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ fwdCurve, Microsoft::FSharp::Core::FSharpOption<DateTime>^ startDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer) : CFloatingRateBond(CCCTEU::typeid)
{
    CYieldTermStructure^ _CfwdCurve;
    try
    {
#ifdef HANDLE
        _phCCTEU = NULL;
#endif
        QuantLib::Date _maturityDate = (QuantLib::Date)ValueHelper::Convert (maturityDate); //d
        QuantLib::Spread _spread = (QuantLib::Spread)ValueHelper::Convert (spread); //d
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (fwdCurve))
        {
            _CfwdCurve = safe_cast<CYieldTermStructure^> (fwdCurve->Value);
            _CfwdCurve->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _fwdCurve = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (fwdCurve) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CfwdCurve->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        QuantLib::Date _startDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (startDate) ? (QuantLib::Date)ValueHelper::Convert (startDate->Value) : QuantLib::Date()); //4
        QuantLib::Date _issueDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (issueDate) ? (QuantLib::Date)ValueHelper::Convert (issueDate->Value) : QuantLib::Date()); //4
        _ppCCTEU = new boost::shared_ptr<QuantLib::CCTEU> (new QuantLib::CCTEU ( _maturityDate,  _spread,  _fwdCurve,  _startDate,  _issueDate ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCCTEU)->setPricingEngine (_QL_Pricer);
        SetFloatingRateBond (boost::dynamic_pointer_cast<QuantLib::FloatingRateBond> (*_ppCCTEU));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfwdCurve != nullptr) _CfwdCurve->Unlock();
    }
}
Cephei::QL::Instruments::Bonds::CCCTEU::CCCTEU (boost::shared_ptr<QuantLib::CCTEU>& childNative, Object^ owner) : CFloatingRateBond(CCCTEU::typeid)
{
#ifdef HANDLE
	_phCCTEU = NULL;
#endif
	_ppCCTEU = &childNative;
    _ppFloatingRateBond = new boost::shared_ptr<QuantLib::FloatingRateBond> (boost::dynamic_pointer_cast<QuantLib::FloatingRateBond> (*_ppCCTEU));
}
Cephei::QL::Instruments::Bonds::CCCTEU::CCCTEU (QuantLib::CCTEU& childNative, Object^ owner) : CFloatingRateBond(CCCTEU::typeid)
{
#ifdef HANDLE
	_phCCTEU = NULL;
#endif
	_ppCCTEU = new boost::shared_ptr<QuantLib::CCTEU> (&childNative);
    _ppFloatingRateBond = new boost::shared_ptr<QuantLib::FloatingRateBond> (boost::dynamic_pointer_cast<QuantLib::FloatingRateBond> (*_ppCCTEU));
    _CCTEUOwner = owner;
    _FloatingRateBondOwner = owner;
}

Cephei::QL::Instruments::Bonds::CCCTEU::CCCTEU (CCCTEU^ copy) : CFloatingRateBond(CCCTEU::typeid)
{
#ifdef HANDLE
	_phCCTEU = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCCTEU = new boost::shared_ptr<QuantLib::CCTEU> (copy->GetShared());
        _ppFloatingRateBond = new boost::shared_ptr<QuantLib::FloatingRateBond> (boost::dynamic_pointer_cast<QuantLib::FloatingRateBond> (*_ppCCTEU));
    }
}
Cephei::QL::Instruments::Bonds::CCCTEU::CCCTEU (PLATFORM::Type^ t) : CFloatingRateBond(CCCTEU::typeid)
{
#ifdef HANDLE
	_phCCTEU = NULL;
#endif
	if (!t->IsSubclassOf(CCCTEU::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::Bonds::CCCTEU::CCCTEU (QuantLib::Handle<QuantLib::CCTEU>& childNative, Object^ owner)  : CFloatingRateBond(CCCTEU::typeid)
{
	_phCCTEU = &childNative;
	_ppCCTEU = &static_cast<boost::shared_ptr<QuantLib::CCTEU>>(childNative.currentLink());
    _ppFloatingRateBond = new boost::shared_ptr<QuantLib::FloatingRateBond> (boost::dynamic_pointer_cast<QuantLib::FloatingRateBond> (*_ppCCTEU));
    _CCTEUOwner = owner;
}
Cephei::QL::Instruments::Bonds::CCCTEU::CCCTEU (QuantLib::Handle<QuantLib::CCTEU> childNative)  : CFloatingRateBond(CCCTEU::typeid)
{
	_phCCTEU = &childNative;
	_ppCCTEU = &static_cast<boost::shared_ptr<QuantLib::CCTEU>>(childNative.currentLink());
    _ppFloatingRateBond = new boost::shared_ptr<QuantLib::FloatingRateBond> (boost::dynamic_pointer_cast<QuantLib::FloatingRateBond> (*_ppCCTEU));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::Bonds::CCCTEU::CCCTEU (QuantLib::CCTEU childNative)  : CFloatingRateBond(CCCTEU::typeid)
{
#ifdef HANDLE
	_phCCTEU = NULL;
#endif
	_ppCCTEU = new boost::shared_ptr<QuantLib::CCTEU> (new QuantLib::CCTEU (childNative));
    _ppFloatingRateBond = new boost::shared_ptr<QuantLib::FloatingRateBond> (boost::dynamic_pointer_cast<QuantLib::FloatingRateBond> (*_ppCCTEU));
}
#endif

Cephei::QL::Instruments::Bonds::CCCTEU::~CCCTEU ()
{
    if (_ppCCTEU != NULL)
    {
	    delete _ppCCTEU;
        _ppCCTEU = NULL;
    }
}
Cephei::QL::Instruments::Bonds::CCCTEU::!CCCTEU ()
{
    if (_ppCCTEU != NULL)
    {
	    delete _ppCCTEU;
    }
}
QuantLib::CCTEU& Cephei::QL::Instruments::Bonds::CCCTEU::GetReference ()
{
    if (_ppCCTEU == NULL) throw REFNEW NativeNullException ();
	return **_ppCCTEU;
}
boost::shared_ptr<QuantLib::CCTEU>& Cephei::QL::Instruments::Bonds::CCCTEU::GetShared ()
{
    if (_ppCCTEU == NULL) throw REFNEW NativeNullException ();
	return *_ppCCTEU;
}
QuantLib::CCTEU* Cephei::QL::Instruments::Bonds::CCCTEU::GetPointer ()
{
    if (_ppCCTEU == NULL) throw REFNEW NativeNullException ();
	return &**_ppCCTEU;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CCTEU>& Cephei::QL::Instruments::Bonds::CCCTEU::GetHandle ()
{
	if (_phCCTEU == NULL)
	{
		_phCCTEU = new Handle<QuantLib::CCTEU> (*_ppCCTEU);
	}
	return *_phCCTEU;
}
#endif
bool Cephei::QL::Instruments::Bonds::CCCTEU::HasNative () 
{
	return (_ppCCTEU != NULL);
}

Double Cephei::QL::Instruments::Bonds::CCCTEU::AccruedAmount (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCCTEU)->accruedAmount ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::Bonds::ICCTEU^ Cephei::QL::Instruments::Bonds::CCCTEU_Factory::Create (DateTime maturityDate, Double spread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ fwdCurve, Microsoft::FSharp::Core::FSharpOption<DateTime>^ startDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CCCTEU ( maturityDate,  spread,  fwdCurve,  startDate,  issueDate,  QL_Pricer);
}
